<template>
  <a-modal :label-col="{style: { width: '200px' }}"
           v-model:visible="visible"
           okText="同意"
           cancelText="取消"
           @ok="submit"
           @cancel="emit('close')"
           title="第一次发布">
    <p>第一次发布非匿名评论，系统将会自动为您注册一个账户，您的账户将用您的邮箱进行标识，
      同时我们将会为您生成一个默认的密码发送至您的邮箱，此后您的评论被回复时也会同时通过邮箱通知您。</p>
  </a-modal>
</template>

<script setup>
import {message} from "ant-design-vue";
import {apiGet, apiPost} from "../../../../../fentch";

const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
    default: false
  },
  username: {
    type: String,
    required: true,
    default: ''
  },
  email: {
    type: String,
    required: true,
    default: ''
  }
})

const emit = defineEmits(['close', 'confirm'])

const submit = () => {
  apiPost('/users/nopwd', {
    username: props.username,
    email: props.email
  }).then(res => {
    if (res.data.code === 0) {
      window.localStorage.setItem('token', res.data.data.access_token)
      // 当前时间
      let now = new Date();
      now.setSeconds(now.getSeconds() + res.data.data.expires_in)
      window.localStorage.setItem('expireTime', now.getTime().toString())
      apiGet('/auth/me').then(res => {
        if (res.data.code === 0) {
          window.localStorage.setItem('user', JSON.stringify(res.data.data))
          emit('confirm')
          return
        }
        message.error(res.data.message)
      })
      return
    }
    message.error(res.data.message)
  })
  emit('close')
}
</script>

<style scoped>

</style>